package net.roseparadise.service.impl;

import java.util.List;

import net.roseparadise.dao.CustomerDAO;
import net.roseparadise.dao.GuestDAO;
import net.roseparadise.dao.MemberDAO;
import net.roseparadise.dao.PasswordDAO;
import net.roseparadise.model.Member;
import net.roseparadise.model.Password;
import net.roseparadise.service.UserService;
import net.roseparadise.util.VOUtils;
import net.roseparadise.vo.UserVO;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
@Service
@Transactional
public class UserServiceImpl implements UserService {
	@Autowired
	private MemberDAO memberDAO;
	@Autowired
	private PasswordDAO passwordDAO;
	@Autowired
	private GuestDAO guestDAO;
	@Autowired
	private CustomerDAO customerDAO;
	
	public boolean checkMemberName(String name){
		boolean result = true;
		try {
			List<Member> memList =memberDAO.findByName(name);
			if( null != memList && memList.size()>0){
				result = false;
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return result;
	}
	
	
	
	public UserVO MemberLogin(String name,String password){	
		UserVO result = null;
		Member member= null;
		try {
			List<Member> memList =memberDAO.findByName(name);
			if(null!= memList && memList.size()>0)
			{	
				member=memList.get(0);
				List list = passwordDAO.findByExample(new Password(member.getId(),password));
				if(null!= list && list.size()>0){
					result = VOUtils.getUserVO(member);
				}
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return result;
	}
	
	public UserVO addMember(Member member,String password){
		UserVO result = null;
		if(this.checkMemberName(member.getName()))
		{
			Member newMem = memberDAO.merge(member);
			Password pw = new Password(newMem.getId(),password);
			passwordDAO.save(pw);
			result = VOUtils.getUserVO(newMem);
		}		
		return result;
	}
	
}
